package org.uim.cricket.storage.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Collection;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.UIManager;

import org.uim.cricket.entities.FullSeries;

/**
 *
 * @author Isuru Madushanka
 */
public class AskNewSeries extends javax.swing.JDialog {

	private static final long serialVersionUID = -7525342466605237589L;
	
	private FullSeries series = null;
	private Collection<String> sTypes = null;
	private String selectedSeriesType = null;
	private int defaultTypeIndex = 0;
	private int totalTeamsCount = 0;
	
	/** Creates new form AskNewCountry */
    
	public AskNewSeries(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        initComponents();

        this.setCenterScreen();
    }

	public AskNewSeries(java.awt.Frame parent, boolean modal, FullSeries series, Collection<String> types, int defaultSel) {
		this(parent, modal);
		
		this.series = series;
		this.sTypes = types;
		
        this.fillData();
	}
	
    private void fillData() {
        this.setTitle("Series data for #" + series.getId());
        
        this.lblID.setText(series.getId());
        this.txtFullname.setText(series.getFullname());
        this.txtSeason.setText(series.getSeason());
        this.setDrawnState(series.isDrawn());
        this.txtTeamWon.setText(series.getTeamWon());
        this.txtWinMargin.setText(series.getMargin());
        this.txtTotalMatches.setText(series.getTotalMatches() + "");
        
        this.addTeams(series.getParticipatedCountries());
        this.addSeriesTypes(sTypes);
    }

    private void addSeriesTypes(Collection<String> seriesTypes) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        int curr = 0;
        for (String type : seriesTypes) {
            model.addElement(type);
            if (this.totalTeamsCount == 2 && type.equalsIgnoreCase("Bilateral")) {
            	this.defaultTypeIndex = curr;
            } else if (this.totalTeamsCount == 3 && type.equalsIgnoreCase("Triangular")) {
            	this.defaultTypeIndex = curr;
            } else if (this.totalTeamsCount >= 8 && type.equalsIgnoreCase("ICC World Cup")) {
            	this.defaultTypeIndex = curr;
            } else if (this.totalTeamsCount > 3 && this.totalTeamsCount < 8 && type.equalsIgnoreCase("Multinational")) {
            	this.defaultTypeIndex = curr;
            }
            curr++;
        }
        this.cmbSeriesType.setModel(model);
        
        if (this.defaultTypeIndex >= 0 && this.defaultTypeIndex < model.getSize()) {
        	this.cmbSeriesType.setSelectedIndex(defaultTypeIndex);
        }
    }

    private void setCenterScreen() {
        Dimension window = this.getSize();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        int newX = (int) (screen.getWidth() - window.getWidth()) / 2;
        int newY = (int) (screen.getHeight() - window.getHeight()) / 2;

        this.setLocation(newX, newY);
    }
    
    private void setDrawnState(boolean isDrawn) {
        if (isDrawn)
            this.cmbDrawn.setSelectedIndex(0);
        else
            this.cmbDrawn.setSelectedIndex(1);
    }

    private void addTeams(Collection<String> teams) {
        DefaultListModel model = new DefaultListModel();
        for (String team : teams) {
            model.addElement(team);
        }
        this.lstTeams.setModel(model);
        this.totalTeamsCount = model.getSize();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblID = new javax.swing.JLabel();
        cmbDrawn = new javax.swing.JComboBox();
        txtTeamWon = new javax.swing.JTextField();
        txtWinMargin = new javax.swing.JTextField();
        txtTotalMatches = new javax.swing.JTextField();
        txtFullname = new javax.swing.JTextField();
        txtSeason = new javax.swing.JTextField();
        lblTeams = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstTeams = new javax.swing.JList();
        cmdOK = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        cmbSeriesType = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("ID:");

        jLabel2.setText("Fullname:");

        jLabel3.setText("Season:");

        jLabel4.setText("Drawn:");

        jLabel5.setText("Team won:");

        jLabel6.setText("Win margin:");

        jLabel7.setText("Total Matches:");

        lblID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblID.setText("id");

        cmbDrawn.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Yes", "No" }));

        txtWinMargin.setText("0-0");

        txtTotalMatches.setText("0");

        lblTeams.setText("Participated Teams:");

        jScrollPane1.setViewportView(lstTeams);

        cmdOK.setText("OK");
        cmdOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOKActionPerformed(evt);
            }
        });

        jLabel8.setText("Series Type:");

        cmbSeriesType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbSeriesType, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblID)
                            .addComponent(txtFullname, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTeamWon, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtWinMargin, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTotalMatches, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(cmbDrawn, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtSeason, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblTeams))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 417, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(346, 346, 346)
                            .addComponent(cmdOK, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtFullname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtSeason, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cmbDrawn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtTeamWon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtWinMargin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtTotalMatches, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbSeriesType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(34, 34, 34)
                .addComponent(lblTeams)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cmdOK)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {
        boolean drawn = (this.cmbDrawn.getSelectedItem().toString().equalsIgnoreCase("Yes"));

        series.setDrawn(drawn);
        if (!this.txtFullname.getText().equals(series.getFullname()) && this.txtFullname.getText().length() > 0) {
        	series.setFullname(this.txtFullname.getText());
        }
        if (!this.txtSeason.getText().equals(series.getFullname()) && this.txtSeason.getText().length() > 0) {
        	series.setSeason(this.txtSeason.getText());
        }
        this.selectedSeriesType = this.cmbSeriesType.getSelectedItem().toString();

        this.setVisible(false);
    }

    public String getSelectedSeriesType() {
    	return this.selectedSeriesType;
    }
    
    public FullSeries getSeries() {
    	return this.series;
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AskNewSeries dialog = new AskNewSeries(null, true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JComboBox cmbDrawn;
    private javax.swing.JComboBox cmbSeriesType;
    private javax.swing.JButton cmdOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblID;
    private javax.swing.JLabel lblTeams;
    private javax.swing.JList lstTeams;
    private javax.swing.JTextField txtFullname;
    private javax.swing.JTextField txtSeason;
    private javax.swing.JTextField txtTeamWon;
    private javax.swing.JTextField txtTotalMatches;
    private javax.swing.JTextField txtWinMargin;
    // End of variables declaration

}
